﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Managements.InterfaceServices;
using YAPACS.Organizations;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Controllers;
[Authorize]
public class InterfaceServiceController : AbpController
{
    private readonly IRepository<Department, Guid> _departmentRepository;
    private readonly ICurrentUserOrganization _currentUserOrganization;

    public InterfaceServiceController(
        IRepository<Department, Guid> departmentRepository,
        ICurrentUserOrganization currentUserOrganization)
    {
        _departmentRepository = departmentRepository;
        _currentUserOrganization = currentUserOrganization;
    }

    [HttpGet]
    [Route("api/get-interface-service")]
    public async Task<IActionResult> GetInterfaceServiceByDepartment()
    {
        try
        {
            var department = await _departmentRepository.GetAsync(_currentUserOrganization.DepartmentId.Value);
            var interfaceServices = department.GetInterfaceServices().ToList();
            return Ok(new WorkflowOutput { Success = true, Data = interfaceServices.Select(e => new { e.Id, e.Name }) });
        }
        catch (Exception ex)
        {
            return Ok(new WorkflowOutput { Success = false, Error = ex.Message });
        }
    }
}
